package com.work.chat.common.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Apaas时间转换器
 */
public class MpaasDateDeserializer extends JsonDeserializer<Date> {
    public MpaasDateDeserializer() {
    }

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try {
            String valueAsString = jsonParser.getValueAsString();
            if (ObjectUtils.isEmpty(valueAsString)){
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(valueAsString);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }
}
